package com.sl.hx.shop.http;

import android.content.Context;
import android.os.Handler;

import com.apkfuns.logutils.LogUtils;
import com.sl.hx.shop.view.dialog.LoadingDailog;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 用于显示进度条和关闭，还有取消订阅的操作
 * <p>
 * Created by hwg on 2017/12/20.
 */

public abstract class ProgressSubscribe<T> implements ProgressCancelListener, Observer<T> {

    private Context mContext;

    private Disposable disposable;

    private LoadingDailog.Builder loadBuilder;
    private  LoadingDailog dialog;

    public ProgressSubscribe(Context context) {
        this.mContext = context;
        initDialog();
    }

    private void initDialog() {
        loadBuilder=new LoadingDailog.Builder(mContext)
                .setMessage("加载中")
                .setCancelable(true)
                .setCancelOutside(true);
        dialog=loadBuilder.create();
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable = d;
    }

    @Override
    public void onComplete() {
        dismissProgressDialog();
    }

    @Override
    public void onError(Throwable e) {
        LogUtils.e(e.getMessage());
//
//        //网络是否可用的判断
//        if (!NetworkUtil.isNetworkAvalible(mContext)) {
//            _onError("网络不可用");
//            ToastUtil.showShort(mContext, "网络不可用");
//
//        } else if (e instanceof ApiException) {
//
//            if (TextUtils.isEmpty(SPUtils.getInstance().getString("token"))) { //统一处理未登录的状态
//
//                //没有token的情况下，最新判断是否是第三方登录
//                if (((ApiException) e).getCode() == ApiException.NO_BAND_USER) { //第三方登录未绑定用户
//
//                    _onError("3000");
//
//                } else {
//
//                    ToastUtil.showShort(mContext, "您还未登录，请先登录");
//
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
////                            Intent intent = new Intent(mContext, LoginActivity.class);
////                            mContext.startActivity(intent);
//                        }
//                    }, 1000);
//                }
//
//            } else if (((ApiException) e).getCode() == ApiException.TOKEN_FAILED) {//统一处理token过期，延迟1秒跳转到登录界面重新登录
//
//                ToastUtil.showShort(mContext, e.getMessage());
//
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
////                        SPUtils.getInstance().remove(SPUKey.TOKEN);
////                        Intent intent = new Intent(mContext, LoginActivity.class);
////                        mContext.startActivity(intent);
//                    }
//                }, 1000);
//
//            } else {
//                ToastUtil.showShort(mContext, e.getMessage());
//            }
//        } else {
            _onError(e.getMessage());
//        }

        dismissProgressDialog();
    }

    @Override
    public void onNext(T t) {
        _onNext(t);
    }


    public void showProgressDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissProgressDialog() {
        if (dialog != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    dialog = null;
                }
            }, 300) ;

            onCancelListener();
        }
    }

    /**
     * 在自定义的loding中调用
     * 用于终止网络请求
     */
    @Override
    public void onCancelListener() {
        if (!disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    protected abstract void _onNext(T t);

    protected abstract void _onError(String message);

}
